﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 地理位置应用服务类
    /// </summary>
    public class LocationAppService : ApplicationService, ILocationAppService
    {
        private readonly IRepository<Location, int> _locationRepository;

        public LocationAppService(
            IRepository<Location, int> locationRepository)
        {
            _locationRepository = locationRepository;
        }

        // 获取当前地理位置
        public async Task<ResultResponse<LocationDto>> GetAsync()
        {
            var location = await _locationRepository
                .OrderBy(m => Guid.NewGuid())
                .Include(m => m.Area)
                .Include(m => m.Area.City)
                .Include(m => m.Area.City.Province)
                .FirstOrDefaultAsync();
            var dto = ObjectMapper.Map<Location, LocationDto>(location);
            return ResultResponse<LocationDto>.Successful(dto);
        }
    }
}